package com.cxf.windex.base.util;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * @author chenxf
 * @date 3/16/2018
 * @description
 */
public class DateUtil {

  /**
   * 获取当前年月
   * @return 201803
   */
   public static String getYearMonth(){
     LocalDate today = LocalDate.now();
     int year = today.getYear();
     int month = today.getMonthValue();
     int day = today.getDayOfMonth();
     return String.format("%d-%02d", year, month);
   }

  /**
   * 获取 年月日期的第1天
   * @param yearMonth 年月
   * @param num 0当月 -1 上月 1下月
   * @return
   */
   public static Timestamp getFirstDay(String yearMonth,int num) throws Exception{
     int year=Integer.valueOf(yearMonth.substring(0,4));
     int month= Integer.valueOf(yearMonth.substring(4,6));
     LocalDateTime ldt1 = LocalDateTime.of(year,month, 1, 0, 0, 0);
     ldt1=ldt1.plusMonths(num);
     return Timestamp.valueOf(ldt1);
   }

   public  static Date getDate(String dateStr){
     ZoneId zoneId = ZoneId.systemDefault();
     ZonedDateTime zdt = getLocalDate(dateStr).atStartOfDay(zoneId);
     return Date.from(zdt.toInstant());
   }

   public static LocalDate getLocalDate(String dateStr){
     String[] datas=dateStr.split("-");
     return  LocalDate.of(Integer.parseInt(datas[0]),Integer.parseInt(datas[1]),Integer.parseInt(datas[2]));
   }

  public static boolean localDateBeforeNow(String dateStr){
    return  LocalDate.now().isAfter(getLocalDate(dateStr));
  }

   public static void main(String[] args){
      System.out.println(localDateBeforeNow("2018-03-27"));
   }

}
